#include "PlayAdjustLayer.h"
#include "../../Level/LevelManager.h"
#include "../../../Objects/Ghost/Play/GhostAdjust.h"
#include "../../../Support/GameSupport.h"

PlayAdjustLayer::PlayAdjustLayer(Size screenSize)
{
	m_screenSize = screenSize;
}
void PlayAdjustLayer::initialize(cocos2d::Node *layerCoco)
{
    this->addChild(layerCoco);

    m_Clound = dynamic_cast<Sprite*>(layerCoco->getChildByName(std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_Cloud))));
    m_Clound->setAnchorPoint(Vec2(0.5f, 0.5f));
    Button* btnSettings = dynamic_cast<Button*>(layerCoco->getChildByName(std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_btnSetting))));
    
    m_loadingBar = dynamic_cast<LoadingBar*>(layerCoco->getChildByName(
                                                                               std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_TimeBarBG)))->getChildByName(
                                                                                                                                                                                         std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_TimeBarFG))));
    
   // auto func = CallFunc::create(std::bind(&PlayManager::calculateTimePlay, this, m_loadingBar));
    
    
    btnIncrease = dynamic_cast<Button*>(layerCoco->getChildByName(std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_btnIncrease))));
    btnDecrease = dynamic_cast<Button*>(layerCoco->getChildByName(std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_btnDecrease))));
    
    btnIncrease->setTitleText("+1");
    btnDecrease->setTitleText("-1");
    btnIncrease->setTitleFontSize(btnIncrease->getContentSize().width / 3);
    btnDecrease->setTitleFontSize(btnDecrease->getContentSize().width / 3);
    btnIncrease->addClickEventListener(CC_CALLBACK_1(PlayAdjustLayer::IncreaseGhost, this));
    btnDecrease->addClickEventListener(CC_CALLBACK_1(PlayAdjustLayer::DecreaseGhost, this));
}
void PlayAdjustLayer::IncreaseGhost(cocos2d::Ref *pSender)
{
    Button* btn = (Button*)pSender;
    int numAdjust = stoi(btn->getTitleText());
    for (int i = 0, indexGhost = 1; i< numAdjust; i++)
    {
        int numRow = (int)m_nodeGhostPlay->getChildrenCount();
        for (int k = 0; k < numRow; k++)
        {
            Node* currRow = m_nodeGhostPlay->getChildByName(cocos2d::String::createWithFormat("%s%d", "Row_", k + 1)->getCString());
            int numGhost = (int)currRow->getChildrenCount();
            for (int j = 0; j< numGhost; j++, indexGhost++)
            {
                GhostAdjust* ghost = (GhostAdjust*)currRow->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getChildren().at(0);
                if (!ghost->isVisible())
                {
                    ghost->setVisible(true);
                    //GamePlayConfig::GetInstance()->m_selectNumber++;
                    numAdjust--;
                    if (numAdjust == 0)
                        return;
                    break;
                }
            }
        }
    }
}

void PlayAdjustLayer::DecreaseGhost(cocos2d::Ref *pSender)
{
    Button* btn = (Button*)pSender;
    int numAdjust = stoi(btn->getTitleText());
    int num = abs(numAdjust);
    for (int i = 0, indexGhost = 1; i< num; i++)
    {
        int numRow = (int)m_nodeGhostPlay->getChildrenCount();
        for (int k = 0; k < numRow; k++)
        {
            Node* currRow = m_nodeGhostPlay->getChildByName(cocos2d::String::createWithFormat("%s%d", "Row_", k + 1)->getCString());
            int numGhost = (int)currRow->getChildrenCount();
            for (int j = 0; j< numGhost; j++, indexGhost++)
            {
                
                GhostAdjust* ghost = (GhostAdjust*)currRow->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getChildren().at(0);
                if (ghost->isVisible())
                {
                    CCLOG("%s", ghost->getName().c_str());
                    ghost->setVisible(false);
                    //GamePlayConfig::GetInstance()->m_selectNumber--;
                    num--;
                    if (num == 0)
                        return;
                    break;
                }
                
            }
            
        }
    }
}

void PlayAdjustLayer::initGame(Node* nodeRoot)
{
    //m_nodeGhostPlay = nodeRoot;
    
    int numRow = (int)nodeRoot->getChildrenCount();
    float canvasHeight = m_screenSize.height / (numRow + 1);
    float expectHeight = canvasHeight*0.6f;
    //int numGhost = cocos2d::random(0,(int)nodeRoot->getChildByName("nodeNormal")->getChildrenCount());
    
    int ghostCount = 0;
    for (int i = 0; i < numRow; i++)
    {
        Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
        int numCol = (int)row->getChildrenCount();
        for (int j = 0; j < numCol; j++)
        {
            ghostCount++;
        }
    }
    
    for (int i = 0,indexGhost = 1; i < numRow; i++)
    {
        Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
        int numCol = (int)row->getChildrenCount();
        for (int j = 0; j < numCol; j++,indexGhost++)
        {
            GhostAdjust* ghost = new GhostAdjust();
            
            switch (LevelManager::s_mode) {
                case kMode_Easy:
                    
                    ghost->initalize(EGhostColor::kGhostColor_Blue, cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), Vec2::ZERO, 1);
                    
                    break;
                case kMode_Normal:
                     ghost->initalize(EGhostColor::kGhostColor_Blue, cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), Vec2::ZERO, 1);
                    break;
                case kMode_Hard:
                     ghost->initalize(EGhostColor::kGhostColor_Blue, cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), Vec2::ZERO, 1);
                    break;
                default:
                    break;
                    
            }
            Node* nodeGhost= row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString());
            nodeGhost->addChild(ghost);
        }
        
    }
    nodeRoot->setVisible(true);
}